/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import cor.EchoHandle;
import cor.ServersHandler;
import general.Agent;
import general.Commands;
import general.MessageHandler;
import general.MessageQueue;
import general.RequestBuilder;
import general.UDPConnection;
import general.UDPMessage;
import general.UDPTestConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author foliveira
 */
public final class UDPServer extends Agent  {
  
  private boolean master;
  private List<String>listLB;           
  private List<String>listSevers; 
  private Map<String,Double>mapEmployee;
  private Map<String,Double>mapConvenat;
  private UDPTestConnection tConnection;  
  private UDPConnection connection;
  private UDPMessage message;
  private RequestBuilder request;
  
  
  public UDPServer(String name,int port, boolean master) {
    
    this.setName(name);
    this.master = master;
    this.setPort(port);
    
    connection = new UDPConnection(port);
    listLB = new ArrayList<String>();
    
    displayMessage(String.format("Agente %s iniciado com a porta %s", getName(), getPort()));
    
    if (this.master){
      System.out.println("master, vai pegar a lista de LoadBalance");
    }
        
    // procura 
    getLoadBalanceEnable();
    
    if(!listLB.isEmpty()){
      getDataServer();
    }
    
    waitForMessages();
    
  }
  
  
  
  public void getDataServer(){
    
    for (Iterator<String> it = listLB.iterator(); it.hasNext();) {
      String host = it.next();      
      
      tConnection = new UDPTestConnection(host);
      
      //
      if (tConnection.connected()){
        
        String msg = String.format("%s:%s:%s:%s:%s:%s", 1,Commands.DATASERVICE,0,"USER","PASSWD",0);
               
        try {
          
          request = new RequestBuilder(msg);
          message = new UDPMessage(connection);
          message.to(tConnection.getHost(), tConnection.getPort());
          message.send(request.getRequest().toString());
          
        } catch (Exception ex) {
          ex.printStackTrace();
        }
        
        break; // śo preciso de resposta
      }
            
    }
          
  }
  
  
  public void displayMessage(String text) {
    System.out.println(text);
  }
  
  public void waitForMessages(){
    
    while (true) {

      try {
        
        message = new UDPMessage(connection);
        String m = message.receive();
        request = new RequestBuilder(m);
        
        if (request.getRequest().getCommand().contains("DATASERVER")){
          listSevers.add(request.getRequest().getData());
        }
        
        
        

      } catch (Exception ex) {
        ex.printStackTrace();
      }

    }

  }
  
  
  
  // solicita alista de Load Balance  
  public void getLoadBalanceEnable(){
    String message;
    
    // possíveis loadBalances
    List<String>list = new ArrayList<String>();
    
    list.add("localhost:9091");
    list.add("localhost:9092");
    list.add("localhost:9093");
    list.add("localhost:9094");
    list.add("localhost:9095");    
    
    
    for (Iterator<String> it = list.iterator(); it.hasNext();) {
      String host = it.next();
      
      
      tConnection = new UDPTestConnection(host);
      
      if (tConnection.connected()){
        listLB.add(host);
      }
      
    }
    
  }
  
  // 
  
}
